﻿using System;
using System.Windows.Input;

namespace YsTiffConverter.ViewModel
{
    /// <summary>
    /// Common class for editing commands.
    /// </summary>
    internal class EditionCommand : ICommand
    {
        /// <summary>
        /// Delegate for event CanExecute.
        /// </summary>
        /// <param name="parameter">Not used.</param>
        /// <returns>True, if a command may be executed.</returns>
        public delegate bool CanExecuteEventHandler(object parameter);

        /// <summary>
        /// Delegate for event Execute.
        /// </summary>
        /// <param name="parameter">Not used</param>
        public delegate void ExecuteEventHandler(object parameter);

        /// <summary>
        /// CanExecute event handler.
        /// </summary>
        public event CanExecuteEventHandler OnCanExecute;

        /// <summary>
        /// Execute event handler.
        /// </summary>
        public event ExecuteEventHandler OnExecute;

        /// <summary>
        /// The method is called when the list view selection was changed.
        /// </summary>
        /// <remarks>
        /// The method raises the event CanExecuteChanged.
        /// </remarks>
        public void SelectedChanged()
        {
            if (null != CanExecuteChanged)
            {
                CanExecuteChanged(this, new EventArgs());
            }
        }

        #region ICommand Members

        public bool CanExecute(object parameter)
        {
            bool rslt = false;
            if (null != OnCanExecute)
            {
                rslt = OnCanExecute(parameter);
            }

            return rslt;
        }

        public event EventHandler CanExecuteChanged;

        public void Execute(object parameter)
        {
            if (null != OnExecute)
            {
                OnExecute(parameter);
            }
        }

        #endregion
    }
}
